// pages/address/address.js
var orderId=null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    phone: '',
    region: [],
    detailAddress: '',
    isCheckDefault: true,
  },

  contactInputChange: function (e) {
    this.setData({
      name: e.detail.value
    })
  },

  phoneInputChange: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  
  detailAddressInputChange: function (e) {
    this.setData({
      detailAddress: e.detail.value
    })
  },

  checkboxChange: function (e) {
    if (e.detail.value.count > 0) {
      this.setData({
        isCheckDefault: true
      })
    } else {
      this.setData({
        isCheckDefault: false
      })
    }
  },

  saveBtnClick: function () {

    var that = this;

    if (this.data.name.length<1) {
      wx.showToast({
        title: '请输入联系人',
        icon: 'none'
      })
      return;
    }
    
    if (!(/^1[34578]\d{9}$/.test(this.data.phone))) {
      wx.showToast({
        title: '请输入正确的联系电话',
        icon: 'none'
      })
      return;
    }
    if (this.data.region.length < 1) {
      wx.showToast({
        title: '请选择地区',
        icon: 'none'
      })
      return;
    }
    if (this.data.detailAddress.length < 1) {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none'
      })
      return;
    }

    wx.request({
      url: getApp().globalData.address_add_url,
      data: {
        userId: getApp().globalData.userInfo.userId,
        isdefault: true,
        contacts: that.data.name,
        phone: that.data.phone,
        province: that.data.region[0],
        city: that.data.region[1],
        county: that.data.region[2],
        address: that.data.detailAddress,
        orderId:orderId
      },
      success: function (res) {
        wx.showToast({
          title: '添加成功',
          complete: function (res) { 
            setTimeout(function (){
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
          },
        })
      },
    })
    

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    orderId = options.orderId;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    wx.request({
      url: getApp().globalData.address_get_url,
      data: {
        userId: getApp().globalData.userInfo.userId,
      },
      success: function (res) {
        console.log(res)
        var addressList = res.data.data.list;
        if (addressList==null || addressList.length<1) return;

        that.setData({
          name: addressList[0].contacts,
          phone: addressList[0].phone,
          region: [addressList[0].province, addressList[0].city, addressList[0].county],
          detailAddress: addressList[0].address,
          isCheckDefault: addressList[0].isdefault==1?true:false,  
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    if (e.from == 'menu') { // 分享小程序

      return {
        title: '快来加入闲置会发光，做最环保的时髦精>>',
        path: '/pages/login/login',
        imageUrl: '/resources/share_miniprogram.png',
      }
    }
  }
})